import { shallowReactive } from "vue";
import { JdkEnvStore } from "../java/jdk/jdk-env-store.ts";
import { MavenEnvStore } from "../java/maven/maven-env-store.ts";
import { MvndEnvStore } from "../java/maven/mvnd-env-store.ts";
import { NodejsEnvStore } from "../nodejs/nodejs-env-store.ts";
import { PythonEnvStore } from "../python/python-env-store.ts";
import { NacosEnvStore } from "@/modules/dev-env/middleware/nacos/nacos-env-store.ts";
import { MysqlEnvStore } from "../database/mysql/mysql-env-store.ts";
import { MongodbEnvStore } from "../database/mongodb/mongodb-env-store.ts";
import { GolangEnvStore } from "../golang/golang-env-store.ts";
import { PgsqlEnvStore } from "../database/pgsql/pgsql-env-store.ts";
import { MariadbEnvStore } from "../database/mariadb/mariadb-env-store.ts";
import { ZkEnvStore } from "@/modules/dev-env/middleware/zookeeper/zk-env-store.ts";
import type { GenericEnvStore } from "@/modules/dev-env/store/generic-env-store.ts";
import { RocketmqEnvStore } from "@/modules/dev-env/middleware/rocketmq/rocketmq-env-store.ts";
import { KafkaEnvStore } from "@/modules/dev-env/middleware/kafka/kafka-env-store.ts";

const factories = {
  "java.jdk": () => new JdkEnvStore(),
  "java.maven": () => new MavenEnvStore(),
  "java.mvnd": () => new MvndEnvStore(),
  "nodejs.sdk": () => new NodejsEnvStore(),
  "python.sdk": () => new PythonEnvStore(),
  "golang.sdk": () => new GolangEnvStore(),

  "db.mysql": () => new MysqlEnvStore(),
  "db.mariadb": () => new MariadbEnvStore(),
  "db.mongodb": () => new MongodbEnvStore(),
  "db.pgsql": () => new PgsqlEnvStore(),

  "md.nacos": () => new NacosEnvStore(),
  "md.zookeeper": () => new ZkEnvStore(),
  "md.rocketmq": () => new RocketmqEnvStore(),
  "md.kafka": () => new KafkaEnvStore(),
};

export type EnvStoresType = typeof factories;
export type EnvStoreId = keyof EnvStoresType;

const storesCache: Partial<Record<EnvStoreId, GenericEnvStore<any>>> = {};

/**
 * 获取或创建环境管理器
 * @param id ID
 */
export function getEnvStore<T extends EnvStoreId>(id: T): ReturnType<EnvStoresType[T]> {
  if (storesCache[id]) {
    return storesCache[id] as ReturnType<EnvStoresType[T]>;
  }
  const store = factories[id]();
  store.id = id;
  const proxy = shallowReactive(store) as ReturnType<EnvStoresType[T]>;
  store.proxy = proxy;
  storesCache[id] = proxy;
  return proxy;
}
